package com.heng.futureDemo;

import org.junit.Test;

import java.util.concurrent.*;

public class ACompletableFutureBuildDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //
        ExecutorService threadPool = Executors.newFixedThreadPool(3);


        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {TimeUnit.SECONDS.sleep(1);}catch (InterruptedException e) {e.printStackTrace();}
            return "hello supplyAsync";
        });

        System.out.println(completableFuture.get());
        threadPool.shutdown();
    }

    //无返回值
    @Test
    public   void m1() throws InterruptedException, ExecutionException {

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {

            System.out.println(Thread.currentThread().getName());

            try {TimeUnit.SECONDS.sleep(1);}catch (InterruptedException e) {e.printStackTrace();}
        },threadPool);

        System.out.println(completableFuture.get());
        threadPool.shutdown();
    }
}
